package com.titan.single.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;

/**
 * 拷贝工具类
 */
@Slf4j
public final class CopyUtils {

    private CopyUtils() {
    }

    // 基于list 进行深拷贝返回,  数据里的对象 必须要implements Serializable 序列化设置
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        List<T> dest = (List<T>) in.readObject();
        return dest;
    }
}
